package pos.domain;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.sql.Driver;
import java.sql.DriverManager;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.zip.Deflater;
import java.io.*;
import java.net.URL;

import pos.data.*;

public class PMNoDao{

    
    private static Properties dbprops = new Properties();
    

    private BufferedWriter esc=null;
    public PMNoDao(){
    	   InputStream is=null;
	       try {
	       	is = Thread.currentThread().getContextClassLoader().getResourceAsStream("dbconfiguration.properties"); 
	       	dbprops.load(is);
	       	dbprops.load(new FileInputStream("/src/dbconfiguration.properties"));
		} catch (Exception e1) {
			System.err.println("El fichero de propiedades de la BDs no se ha encontrado");
			e1.printStackTrace();
		}

    }
    
	public PMNoDao(String dburi, String drivername, String password, String username) throws IOException
    {
		try{
		
		String path=(String)Thread.currentThread().getContextClassLoader ().getResource("dbconfiguration.properties").getPath();
	
		String [ ] palabra = path.split("\\/");
		String pathAux="";
		int j=0;
		while(j <= palabra.length-2){
			pathAux=pathAux+palabra[j]+"/";
		j++;
		}

		
		
		
		
		java.util.Properties dbprops = new java.util.Properties();
    	dbprops.setProperty("dBUri", dburi);
    	dbprops.setProperty("driverName", drivername);
    	dbprops.setProperty("password", password);
    	dbprops.setProperty("username", username);
    	
    	
    	FileOutputStream fos = new FileOutputStream(path);
    	dbprops.store(fos,path);
    
  	
  	fos.close();
  	
    } catch (IOException e) { 
        e.printStackTrace(); 
    } 

    }
	
	public String getDburi() {
	       String uri="";
	       InputStream is=null;
	       try {
	       	is = Thread.currentThread().getContextClassLoader().getResourceAsStream("dbconfiguration.properties"); 
	       	dbprops.load(is);
	       	dbprops.load(new FileInputStream("/src/dbconfiguration.properties"));
		} catch (Exception e1) {
			System.err.println("El fichero de propiedades de la BDs no se ha encontrado");
			e1.printStackTrace();
		}

	     try {
	    	 uri =dbprops.getProperty("dBUri");
	    	 
	        } catch (Exception e) {
	            System.err.println("Unable to register JDBC Driver");
	            e.printStackTrace();
	        }
	        return uri;
	
	}

	public String getDrivername() {
	       String uri="hola";
	       InputStream is=null;
	       try {
	       	is = Thread.currentThread().getContextClassLoader().getResourceAsStream("dbconfiguration.properties"); 
	       	dbprops.load(is);
	       	dbprops.load(new FileInputStream("/src/dbconfiguration.properties"));
		} catch (Exception e1) {
			System.err.println("El fichero de propiedades de la BDs no se ha encontrado");
			e1.printStackTrace();
		}

	     try {
	    	 uri=dbprops.getProperty("driverName");
	    	 // is.close();
	    	 
	        } catch (Exception e) {
	            System.err.println("Unable to register JDBC Driver");
	            e.printStackTrace();
	        }
	        return uri;
	
	}

	public String getPassword() {
	       String uri="hola";
	       InputStream is=null;
	       try {
	       	is = Thread.currentThread().getContextClassLoader().getResourceAsStream("dbconfiguration.properties"); 
	       	dbprops.load(is);
	       	dbprops.load(new FileInputStream("/src/dbconfiguration.properties"));
		} catch (Exception e1) {
			System.err.println("El fichero de propiedades de la BDs no se ha encontrado");
			e1.printStackTrace();
		}

	     try {
	    	 uri=dbprops.getProperty("password");
	    	// is.close();
	    	 
	        } catch (Exception e) {
	            System.err.println("Unable to register JDBC Driver");
	            e.printStackTrace();
	        }
	        return uri;
	
	}

	public String getUsername() {
	       String uri="";
	       InputStream is=null;
	       try {
	       	is = Thread.currentThread().getContextClassLoader().getResourceAsStream("dbconfiguration.properties"); 
	       	dbprops.load(is);
	       	dbprops.load(new FileInputStream("/src/dbconfiguration.properties"));
		} catch (Exception e1) {
			System.err.println("El fichero de propiedades de la BDs no se ha encontrado");
			e1.printStackTrace();
		}

	     try {
	    	 uri=dbprops.getProperty("username");
	    	// is.close();
	    	 
	        } catch (Exception e) {
	            System.err.println("Unable to register JDBC Driver");
	            e.printStackTrace();
	        }
	        return uri;
	
	}

	
	public String dame() {
	       String uri="hola";
	       InputStream is=null;
	       try {
	       	is = Thread.currentThread().getContextClassLoader().getResourceAsStream("dbconfiguration.properties"); 
	       	dbprops.load(is);
	       	dbprops.load(new FileInputStream("/src/dbconfiguration.properties"));
		} catch (Exception e1) {
			System.err.println("El fichero de propiedades de la BDs no se ha encontrado");
			e1.printStackTrace();
		}

	     try {
	    	 uri =dbprops.getProperty("dBUri");
	    	 uri=uri+dbprops.getProperty("driverName");
	    	 uri=uri+dbprops.getProperty("password");
	    	 uri=uri+dbprops.getProperty("username");
	    	// is.close();
	    	 
	        } catch (Exception e) {
	            System.err.println("Unable to register JDBC Driver");
	            e.printStackTrace();
	        }
	        return uri;
	
	}
	

	
	
	
}

